package com.hgz.jdbcDemo.utils;

import com.hgz.jdbcDemo.dao.StaffsDAO;
import com.hgz.jdbcDemo.dao.impl.StaffsDAOImpl;
import com.hgz.jdbcDemo.pojo.Staffs;
import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author 小志
 * @date 2025/2/24 - 22:21
 */
public class SystemUtil {
    /**
     * Staffs数据访问对象
     */
    private static final StaffsDAO STAFFS_DAO = new StaffsDAOImpl();
    /**
     * 控制台输入对象
     */
    private static final Scanner SCANNER = new Scanner(System.in);
    /**
     * 查询操作
     */
    public static void selectOperation(){
        List<Staffs> staffs = STAFFS_DAO.getStaffsList();
        print("编号  姓名  年龄   联系电话    职位    入职时间    上次更新时间");
        for (Staffs staff : staffs) {
            print(staff.getId()+"  " +staff.getName()+"  "+staff.getAge()+"  "+staff.getPhone()+" "+staff.getStaPos()+"  "+staff.getAddTime()+"  "+staff.getUpdateTime());
        }
    }

    /**
     * 添加操作
     */
    public static void addOperation(){
        boolean flag = true;
        while (flag){
            Staffs staffs = null;
            print("\n-----------添加员工信息-----------");
            System.out.print("请输入员工名字:");
            String name = SCANNER.next();
            System.out.print("请输入员工年龄:");
            Integer age = 0;
            try {
                age = Integer.valueOf(SCANNER.next());
            }catch (Exception e){
                print("请输入整数");
                continue;
            }
            System.out.print("请输入员工电话号码:");
            String phone = SCANNER.next();
            System.out.print("请输入员工职位:");
            String pos = SCANNER.next();
            staffs = new Staffs(name, age, phone, pos);
            if(STAFFS_DAO.addStaffs(staffs) > 0){
                print("添加员工成功");
            }else{
                print("添加员工失败");
            }
            flag = false;
        }
    }

    /**
     * 修改操作
     */
    public static void updateOperation(){
        boolean flag = true;
        while (flag){
            Staffs staffs = null;
            print("\n-----------修改员工信息-----------");
            selectOperation();
            System.out.print("请输入要修改的员工编号:");
            Integer id = 0;
            try {
                id = Integer.valueOf(SCANNER.next());
            }catch (Exception e){
                print("请输入整数");
                continue;
            }
            System.out.print("请输入员工新名字(输入0跳过):");
            String name = SCANNER.next();
            if(name.equals("0")){
                name = null;
            }
            System.out.print("请输入员工新年龄(输入0跳过):");
            Integer age = 0;
            try {
                age = Integer.valueOf(SCANNER.next());
            }catch (Exception e){
                print("请输入整数");
            }
            if(age == 0){
                age = null;
            }
            System.out.print("请输入员工新电话号码(输入0跳过):");
            String phone = SCANNER.next();
            if(phone.equals("0")){
                phone = null;
            }
            System.out.print("请输入新员工职位(输入0跳过):");
            String pos = SCANNER.next();
            if(pos.equals("0")){
                pos = null;
            }
            staffs = new Staffs(id,name, age, phone, pos);
            if(STAFFS_DAO.updateStaffs(staffs)){
                print("修改员工成功");
            }else{
                print("修改员工失败");
            }
            flag = false;
        }
    }

    /**
     * 删除操作
     */
    public static void delOperation(){
        print("\n-----------删除-----------");
        //渲染一遍数据
        selectOperation();
        System.out.print("请输入要删除的序号:");
        String next = SCANNER.next();
        try {
            if(next != null ){
                int i = Integer.parseInt(next);
                if(STAFFS_DAO.deleteStaffs(i)){
                    print("删除当前员工成功");
                }else {
                    print("没有当前员工");
                }
            }
        }catch (NumberFormatException e){
            print("必须输入序号");
        }
    }

    /**
     * 简化输出
     * @param str 输出的文本
     */
    public static void print(String str){
        System.out.println(str);
    }
}
